package com.lee.boot.head.first.demo.redis.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

/**
 * @author: Lee
 * @Date: 2019/11/5 0005 21:31
 * @Description:
 */

@Component
public class RedisConfig {
//    @Bean(name = "RedisConnectionFactory")
//    public RedisConnectionFactory initRedisConnectionFactory() {

//    }

    @Bean
    @Primary
    public RedisTemplate<String, Object> initRedisTemplate(@Autowired RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate();

        //定义序列化器
        RedisSerializer stringSerializer = redisTemplate.getStringSerializer();
        GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
//        redis中value为string 的key使用序列化器
        redisTemplate.setKeySerializer(genericFastJsonRedisSerializer);
        redisTemplate.setValueSerializer(genericFastJsonRedisSerializer);
        redisTemplate.setHashKeySerializer(genericFastJsonRedisSerializer);
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }
}
